//
//  RHPostViewController.swift
//  微博新浪
//
//  Created by hezerong on 16/6/20.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit

class RHPostViewController: UIViewController {
    
    //导航栏取消 + 发布按钮
    var cancelButton:UIButton?
    var sendButton:UIButton?
    
    //toolBar
    lazy var toolBar:UIToolbar = UIToolbar()
    
    //文字编辑view
    lazy var textView:RHComposeView = RHComposeView()
    

    override func viewDidLoad() {
        super.viewDidLoad()


        //调用UI设置
        settingUI()
        
        

    }

 
    func settingUI(){
    
      view.backgroundColor = UIColor.whiteColor()
        
        //导航栏
        setNavigationBarButton()
        
        //标签栏
        setToolBarButton()
        
        //文字view
        addTextView()
        
        //添加通知，监听键盘弹出
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardDidChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
    }
    
    //导航栏按钮设置
    func setNavigationBarButton(){
    
       //创建左侧取消按钮
        cancelButton = UIButton(titlt: "Cancel", titleColor: UIColor.blackColor(), font: 15, image: nil, backGroundImage: "", target: self, action: "cancelButtonFunction:", event: UIControlEvents.TouchUpInside)
        cancelButton?.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        


        cancelButton?.frame = CGRectMake(0, 0, 50, 30)
        let cancelBarButton = UIBarButtonItem(customView: cancelButton!)
        navigationItem.leftBarButtonItem = cancelBarButton
//        cancelButton?.sizeToFit()


        
        //创建右侧发送按钮
        sendButton = UIButton(titlt: "Send", titleColor: UIColor.whiteColor(), font: 15,  backGroundImage: "common_button_orange", target: self, action: "sendButtonFunction:", event: UIControlEvents.TouchUpInside)
        //背景高亮状态
        sendButton?.setBackgroundImage(UIImage(named:"common_button_orange_highlighted" ), forState: .Highlighted)
        //当没有文字的时候，不可点击的状态
        sendButton?.setTitleColor(UIColor.grayColor(), forState: .Disabled)
        sendButton?.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Disabled)

        
        let sendBarButton = UIBarButtonItem(customView: sendButton!)
        sendButton?.frame = CGRectMake(0, 0, 50, 30)
        navigationItem.rightBarButtonItem = sendBarButton
        sendButton?.enabled = false
        
        
        //设置导航栏中间标题
        
        //1.初始化一个lable
        let titleLable = UILabel()
        
        //设置属性字符串
        let titleText = NSMutableAttributedString(string: "发布微博\n", attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(14),NSForegroundColorAttributeName:UIColor.blackColor()])
        //用户名
        let userNameText = NSAttributedString(string: UserAccount.sharedUserAccunt.screen_name!, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12),NSForegroundColorAttributeName:UIColor.grayColor()])

         titleText.appendAttributedString(userNameText)
        
         titleLable.attributedText = titleText
        //自动换行
         titleLable.numberOfLines = 0
        //居中显示
         titleLable.textAlignment = .Center
        //标题自适应
         titleLable.sizeToFit()
        
        navigationItem.titleView = titleLable
        
    }

  
    //设置ToolBar按钮
    func setToolBarButton(){
    
        //把toolbar添加到控制器的view中
       view.addSubview(toolBar)
        
        //布局toolbar
        toolBar.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(view)
            make.right.equalTo(view)
            make.bottom.equalTo(view)
            make.height.equalTo(44)
        }
        
        //数组保存toolbar 按钮图片 和点击方法
        let buttonArray = [
            ["backgroundImage":"compose_toolbar_picture","action":"addPicture:"],
            ["backgroundImage":"compose_mentionbutton_background","action":"atSomeBody:"],
            ["backgroundImage":"compose_trendbutton_background","action":"createTopic:"],
            ["backgroundImage":"compose_emoticonbutton_background","action":"AddEmojiIcon:"],
            ["backgroundImage":"compose_add_background","action":"add:"]
            ]
        
        //初始化UIBarButtonItem
        toolBar.items = [UIBarButtonItem]()
        
        //遍历图片按钮
        for toolBarPictures in buttonArray{
        
           let button = UIButton(titlt: "" ,backGroundImage: toolBarPictures["backgroundImage"], target: self, action: toolBarPictures["action"])
            
            button.sizeToFit()
            
            let barbuttonItem = UIBarButtonItem(customView: button)
            
            //添加
            toolBar.items?.append(barbuttonItem)
            
            //添加弹簧button
            let flexibleButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
            


           //添加到toolbar中
            toolBar.items?.append(flexibleButton)
            
            
        }
        
        //移除最后一个flexibleSpace
        toolBar.items?.removeLast()
        
    }
    
    
    //信息编辑view
    func addTextView(){
    
         view.addSubview(textView)
         textView.font = UIFont.systemFontOfSize(14)
        
//        textView.placeHolder = "who the fuck you think you are talking to?"  
        
        //布局
        textView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_topLayoutGuideBottom)
            make.left.equalTo(view)
            make.right.equalTo(view)
            make.bottom.equalTo(toolBar.snp_top)
        }
    }

}

extension RHPostViewController{

   @objc private func cancelButtonFunction (button:UIButton){
    
       print("取消")
    }
    
   @objc private func sendButtonFunction(button:UIButton){
      
        print("发送")
    }
    
    //添加图片
    @objc private func  addPicture(button:UIButton){
      
        print("添加图片")
    }
    
    //@博友
    @objc private func atSomeBody(button:UIButton){
      
        print("@博友")
    }
    
    //创建话题--发送微博
    @objc private func createTopic(button:UIButton){
    
       print("发微博")
    }
    
    
    //添加表情符
    @objc private func AddEmojiIcon(button:UIButton){
     
        print("添加表情")
    }
    
    //更多功能
    @objc private func add(button:UIButton){
       
        print("更多功能")
    }
    
    //键盘监听通知
    @objc private func keyBoardDidChange(notification:NSNotification){
    
//        print(notification)
        
        let keyBoardFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        
        let duration = (notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue

        
        let keyBoardY = keyBoardFrame.origin.y
        
        let offset = keyBoardY - screenHeight
    
        
        //更新toolbar的 frame 约束
       toolBar.snp_updateConstraints { (make) -> Void in
           make.bottom.equalTo(view).offset(offset)
        }
        
        //添加动画
        UIView.animateWithDuration(duration) { () -> Void in
            self.view.layoutIfNeeded()
        }
        
    }
}